import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'basehomepage.dart';
import 'baseminepage.dart';
import 'grid.dart';
import 'home.dart';

/*
*1、navigatorKey.currentState相当于Navigator.of(context)
*2、home:进入程序后显示的第一个页面,传入的是一个Widget，但实际上这个Widget需要包裹一个Scaffold以显示该程序使用Material Design风格
*3、routes:声明程序中有哪个通过Navigation.of(context).pushNamed跳转的路由参数以键值对的形式传递，key:路由名字，value:对应的Widget
*4、initialRoute:初始路由，当用户进入程序时，自动打开对应的路由。(home还是位于一级)传入的是上面routes的key跳转的是对应的Widget（如果该Widget有Scaffold.AppBar,并不做任何修改，左上角有返回键）
*5、onGenerateRoute:当通过Navigation.of(context).pushNamed跳转路由时，在routes查找不到时，会调用该方法
*
* 详细讲解：https://www.jianshu.com/p/1d44ae246652
* */
class MaterialAppDemo extends StatelessWidget {
  GlobalKey<NavigatorState> _navigatorKey = new GlobalKey();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title:'标题',
      navigatorKey: _navigatorKey,
      home: MaterialAppHome(),
      routes: {
        '/home': (BuildContext context) => BaseHomePageDemo(),
        '/home/grid': (BuildContext context) => BaseMinePageDemo(),
      },
      //默认进入跳转页面
      initialRoute: '/home',
      onGenerateRoute: (setting) {
        //setting.isInitialRoute; bool类型 是否初始路由
        //setting.name; 要跳转的路由名key
        return PageRouteBuilder( pageBuilder: (BuildContext context, _, __) {
          return BaseHomePageDemo();
        },
          opaque: false,
          //跳转动画
          transitionDuration: Duration(milliseconds: 200),
          transitionsBuilder: (  (___, Animation<double> animation, ____, Widget child) {
            return FadeTransition(
                opacity: animation,
              child: ScaleTransition(
                scale: Tween<double>(begin: 0.5,end: 1.0).animate(animation),
                child: child,
              ),
            );

          })

        );
      },
      navigatorObservers: [MyObserver()],
      builder: (context, child){
        //child可设置多个
        // child = easyload(context, child);
        child = Scaffold(
          // Global GestureDetector that will dismiss the keyboard
            body: GestureDetector(
              onTap: () => hideKeyboard(context),
              child: child,
            ));
        return child;
      },
    );
  }
}

class MaterialAppHome extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => MaterialAppHomeState();
}

class MaterialAppHomeState extends State<MaterialAppHome> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('MaterialApp'),
        centerTitle: true,
      ),
      body: Center(
        child: Text('进入默认展示HomePage页面'),
      ),
    );
  }
}

//继承NavigatorObserver
class MyObserver extends NavigatorObserver{
 @override
  void didPush(Route route, Route? previousRoute) {
    // TODO: implement didPush
    super.didPush(route, previousRoute);
    //可通过route.settings获取路由相关内容
    //route.currentResult获取返回内容
    //....等等
    print(route.settings.name);
  }
}
//点击空白区域隐藏键盘
void hideKeyboard(BuildContext context) {
  FocusScopeNode currentFocus = FocusScope.of(context);
  if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
    FocusManager.instance.primaryFocus!.unfocus();
  }
}